/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;

import java.util.Date;
import model.CarBooking;
import model.PackageTour;
import model.TourBooking;
import ws.Bank;

/**
 *
 * @author khanhanh
 */
public class BankBusiness {

    private String CCNumAdmin = "0000000000";
    private String PINCodeAdmin = "1234";
    private TourBusiness tourBiz = new TourBusiness();
    private CarBusiness carBiz = new CarBusiness();
    private LogicBusiness logicBiz = new LogicBusiness();
    private Date toDay = new Date();

    private static Bank getBankDetails(java.lang.String ccNumber, java.lang.String pinCode) {
        ws.BankServiceService service = new ws.BankServiceService();
        ws.BankService port = service.getBankServicePort();
        return port.getBankDetails(ccNumber, pinCode);
    }

    private static Bank getBankDetailsByCusID(int customerID) {
        ws.BankServiceService service = new ws.BankServiceService();
        ws.BankService port = service.getBankServicePort();
        return port.getBankDetailsByCusID(customerID);
    }

    private static boolean deposit(java.lang.String ccNumber, java.lang.String pinCode, float amount) {
        ws.BankServiceService service = new ws.BankServiceService();
        ws.BankService port = service.getBankServicePort();
        return port.deposit(ccNumber, pinCode, amount);
    }

    private static boolean withdraw(java.lang.String ccNumber, java.lang.String pinCode, float amount) {
        ws.BankServiceService service = new ws.BankServiceService();
        ws.BankService port = service.getBankServicePort();
        return port.withdraw(ccNumber, pinCode, amount);
    }

    public Bank getBank(String ccNumber, String pinCode) {
        return getBankDetails(ccNumber, pinCode);
    }

    public boolean deposit_bank(String ccNumber, String pinCode, float amount) {
        return deposit(ccNumber, pinCode, amount);
    }

    public boolean withdraw_bank(String ccNumber, String pinCode, float amount) {
        return withdraw(ccNumber, pinCode, amount);
    }

    public boolean reFundsTour(int packageTourID, int tourBookingID, int customerID) {
        try {
            PackageTour pkTour = tourBiz.objectPkTour(packageTourID);
            TourBooking tourBooking = tourBiz.getTourBookingByID(tourBookingID);
            Bank customerBank = getBankDetailsByCusID(customerID);

            float priceTourBooking = tourBooking.getPrice();
            Date dateStart = pkTour.getFdate();
            int numberDay = logicBiz.dateDiff(toDay, dateStart);
            switch (numberDay) {
                case 1:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceTourBooking * 75 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceTourBooking * 75 / 100);
                    return true;
                case 2:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceTourBooking * 80 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceTourBooking * 80 / 100);
                    return true;
                case 3:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceTourBooking * 85 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceTourBooking * 85 / 100);
                    return true;
                case 4:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceTourBooking * 90 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceTourBooking * 90 / 100);
                    return true;
                default:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceTourBooking * 95 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceTourBooking * 95 / 100);
                    return true;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public boolean reFundCar(int customerID, int carBookingID) {
        try {
            CarBooking carBooking = carBiz.getCarBookingByID(carBookingID);
            Bank customerBank = getBankDetailsByCusID(customerID);

            float priceCarBooking = carBooking.getPrice();
            Date dateStart = carBooking.getPickUpDate();
            int numberDay = logicBiz.dateDiff(toDay, dateStart);

            switch (numberDay) {
                case 1:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceCarBooking * 75 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceCarBooking * 75 / 100);
                    return true;
                case 2:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceCarBooking * 80 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceCarBooking * 80 / 100);
                    return true;
                case 3:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceCarBooking * 85 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceCarBooking * 85 / 100);
                    return true;
                case 4:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceCarBooking * 90 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceCarBooking * 90 / 100);
                    return true;
                default:
                    deposit(customerBank.getCreditCardNumber(), customerBank.getPinCode(), priceCarBooking * 95 / 100);
                    withdraw(CCNumAdmin, PINCodeAdmin, priceCarBooking * 95 / 100);
                    return true;
            }
        } catch (Exception e) {
            return false;
        }
    }
}
